﻿using PivalSoft.LDMS.Data;
using PivalSoft.LDMS.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PivalSoft.LDMS
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserName.Text))
                MessageBox.Show(Resources.RequiredUserName, Resources.MessageCaption);

            else if (string.IsNullOrEmpty(txtPassword.Text))
                MessageBox.Show(Resources.RequiredPassword, Resources.MessageCaption);
            else
            {
                LDMSRepository repository = new LDMSRepository();
                Login login = repository.ValidateLogin(txtUserName.Text.Trim(), txtPassword.Text.Trim());
                if (login != null && login.LoginID > 0)
                {

                    this.Hide();
                    MDIParentfrm f = new MDIParentfrm();
                    f.DisplayName = login.DisplayName;
                    f.UserId = login.LoginID;
                    f.ShowDialog();
                }
                else
                    MessageBox.Show(Resources.LoginFailed, Resources.MessageCaption);
            }
        }

        private int ValidateLogin()
        {
            int retVal = 0;
            if (string.IsNullOrEmpty(txtUserName.Text))
                MessageBox.Show(Resources.RequiredUserName, Resources.MessageCaption);

            if (string.IsNullOrEmpty(txtPassword.Text))
                MessageBox.Show(Resources.RequiredPassword, Resources.MessageCaption);

            LDMSRepository repository = new LDMSRepository();
            Login login = repository.ValidateLogin(txtUserName.Text.Trim(), txtPassword.Text.Trim());
            if (login.LoginID > 0)
                retVal = login.LoginID;
            return retVal;
        }
    }
}
